package com.shixj.easy.spider;

import com.google.common.base.Strings;
import com.shixj.ef.constant.StringCons;
import com.shixj.ef.utils.FetchHelper;

/**
 * Created by shixj on 2018/1/16.
 */
public class Util {
    public static Long MINUS_ONE=-1L;
    public static Long tranWordsToNum(String str) {
        Long num = 0L;
        if (!Strings.isNullOrEmpty(str)) {
            str = FetchHelper.subByRegex(" "+str,
                    "(?<=[^十百千万一二两三四五六七八九零\\d]+)(十|百|千|万|一|二|两|三|四|五|六|七|八|九|零|\\d)+(?=[^十百千万一二两三四五六七八九零\\d]*)",0);
            if (str.matches("^\\d+$"))
                num = Long.valueOf(str);
            else if (str.matches("^(十|百|千|万|一|二|两|三|四|五|六|七|八|九|零)*$")) {
                str=str.replaceAll("零", StringCons.EMPTY);
                Long tempNum = MINUS_ONE;
                char[] words = str.toCharArray();
                boolean isError = false;
                for (char word : words) {
                    switch (word) {
                        case '一':
                            if (tempNum == MINUS_ONE)
                                tempNum = 1L;
                            else isError = true;
                            break;
                        case '两':
                            if (tempNum == MINUS_ONE)
                                tempNum = 2L;
                            else isError = true;
                            break;
                        case '二':
                            if (tempNum == MINUS_ONE)
                                tempNum = 2L;
                            else isError = true;
                            break;
                        case '三':
                            if (tempNum == MINUS_ONE)
                                tempNum = 3L;
                            else isError = true;
                            break;
                        case '四':
                            if (tempNum == MINUS_ONE)
                                tempNum = 4L;
                            else isError = true;
                            break;
                        case '五':
                            if (tempNum == MINUS_ONE)
                                tempNum = 5L;
                            else isError = true;
                            break;
                        case '六':
                            if (tempNum == MINUS_ONE)
                                tempNum = 6L;
                            else isError = true;
                            break;
                        case '七':
                            if (tempNum == MINUS_ONE)
                                tempNum = 7L;
                            else isError = true;
                            break;
                        case '八':
                            if (tempNum == MINUS_ONE)
                                tempNum = 8L;
                            break;
                        case '九':
                            if (tempNum == MINUS_ONE)
                                tempNum = 9L;
                            else isError = true;
                            break;
                        case '十':
                            num +=  Math.abs(tempNum) * 10;
                            tempNum = MINUS_ONE;
                            break;
                        case '百':
                            num +=  Math.abs(tempNum) * 100;
                            tempNum = MINUS_ONE;
                            break;
                        case '千':
                            num += Math.abs(tempNum)* 1000;
                            tempNum = MINUS_ONE;
                            break;
                        case '万':
                            num +=  Math.abs(tempNum) * 10000;
                            tempNum = MINUS_ONE;
                            break;
                        case '亿':
                            num +=  Math.abs(tempNum) * 10000 * 10000;
                            tempNum = MINUS_ONE;
                            break;
                    }
                    if (isError)
                        break;
                }
                if (tempNum>MINUS_ONE)
                num += tempNum;
            }
        }
        return num;
    }
}
